package cn.simon.controller;


import cn.simon.model.Address;
import cn.simon.service.AddressService;
import cn.simon.vo.AddressVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.validation.Valid;

import java.util.List;


@Controller
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 查询所有地址
     * @return
     */
    @GetMapping("/getAddress")
    public String findAll(Model model){
        List<Address> addressList = addressService.findAll();
        model.addAttribute("addressList",addressList);
        return "getAddress";

    }

    /**
     * 添加地址
     * @return
     */
    @GetMapping("/addAddress")
    public String add(){
        return "addAddress";
    }

    @PostMapping("/addAddress")
    public String add(@Valid AddressVO addressVO, BindingResult result, Model model){
        Address address = new Address();
        BeanUtils.copyProperties(addressVO,address);
        addressService.add(address);
        return "redirect:/address/addAddress";
    }
    @GetMapping("/money")
    public String findBiId(Integer id,Model model){
        List<Address> addressList = addressService.findById(id);
        model.addAttribute("addressList",addressList);
        return "money";


    }
}
